package com.example.s.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.s.Activity.UserDetailActivity;
import com.example.s.Activity.UserInfoActivity;
import com.example.s.Activity.WorksDetailActivity;
import com.example.s.Activity.setting.SettingActivity;
import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.adapter.MyRecyclerViewAdapter;
import com.example.s.application.MyApplication;
import com.example.s.bean.RecContent;
import com.example.s.bean.User;
import com.example.s.utils.LiveDataBus;
import com.example.s.view.MyNav;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class MyFragment extends BaseFragment {
    private ImageView setting;
    private ImageView info;
    private CircleImageView headphoto;
    private TextView introduction0;
    private TextView userName;
    private TextView introduction;
    private TextView label;
    private TextView likesAndCollctions;
    private TextView fans;
    private TextView follows;
    private ImageView share;
    private TextView userId;
    private TabLayout nav;
    private ViewPager vp;
    private ScrollView sv;
    private List<List<RecContent>> lists = new ArrayList<>();


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //初始化用户信息
        User user = MyApplication.getInstance(A).getUser();
        if (user != null) {
            userName.setText(user.getNickName());
            fans.setText(String.valueOf(user.getFans()));
            follows.setText(String.valueOf(user.getFollowNum()));
            likesAndCollctions.setText(String.valueOf(user.getLikes()));
            introduction0.setText("".equals(user.getIntroduce()) ? "这个人有点小懒...还没有介绍" : user.getIntroduce());
            Glide.with(A).load(user.getHeadPhoto()).into(headphoto);
            userId.setText(user.getAccount());
        }

        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
            }
        });
        info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(getActivity(), UserInfoActivity.class));
            }
        });


        getTrensItemData();

        ArrayList<MyNav.NavItem> navItems = new ArrayList<>();
        navItems.add(new MyNav.NavItem("动态"));
        navItems.add(new MyNav.NavItem("收藏"));
        navItems.add(new MyNav.NavItem("赞过"));


        new MyNav<List<RecContent>, RecContent>(nav, vp, navItems, new MyNav.OnMyNavListener() {
            @Override
            public void onCommonListener(ImageView icon, TextView title) {
                title.setTextColor(Color.parseColor("#FFB5B5B5"));
                title.setTextSize(16);
            }

            @Override
            public void onSelectListener(ImageView icon, TextView title, int pos) {
                title.setTextColor(Color.parseColor("#ffffff"));
            }
        })
                .setVpRecyclerData(new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL), lists, R.layout.rec_recycler_item)
                .setRecyclerCallBack(new MyNav.OnRecyclerCallBack<RecContent>() {
                    @Override
                    public void handleRecyclerData(RecyclerView view, MyRecyclerViewAdapter<RecContent> adapter, InnerHolder holder, RecContent item) {
                        Log.d(TAG, "handleData: " + item);
                        holder.setSrcResource(R.id.headphoto, item.getHeadPhoto());
                        holder.setSrcResource(R.id.contentPhoto, item.getContentPhoto());
                        holder.setText(R.id.title, item.getTitle());
                        holder.setText(R.id.subtitle, item.getSubTitle());
                        holder.setText(R.id.likes, String.valueOf(item.getLikes()));
                        holder.getView(R.id.follow).setVisibility(View.INVISIBLE);
                        holder.setOnClickListener(R.id.like, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //点赞操作
                            }
                        });
                        holder.setOnClickListener(R.id.headphoto, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                LiveDataBus.getInstance().with("currentUser", RecContent.class).setValue(item);
                                Intent intent = new Intent(getActivity(), UserDetailActivity.class);
                                startActivity(intent);
                            }
                        });
                        holder.setOnClickListener(R.id.contentPhotoContainer, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                LiveDataBus.getInstance().with("currentWorkDetail", RecContent.class).setValue(item);
                                Intent intent = new Intent(getActivity(), WorksDetailActivity.class);
                                startActivity(intent);
                            }
                        });

                    }


                });


        /**
         * 标题栏吸顶
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            final boolean[] flag = {false};
            final float[] y = {0}; //joke 导航的y
            sv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @SuppressLint("ResourceAsColor")
                @Override
                public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    Log.d(TAG, "onScrollChange: ");
                    if (!flag[0]) {
                        y[0] = nav.getY();
                        flag[0] = true;
                    }
                    if (oldScrollY > y[0]) {
                        LiveDataBus.getInstance().with("style", Integer.class).setValue(0);
                        nav.setBackgroundColor(Color.parseColor("#000038"));
                        nav.setY(scrollY);
                    } else {
                        //开始吸顶
                        LiveDataBus.getInstance().with("style", Integer.class).setValue(4);
                        nav.setBackgroundColor(android.R.color.transparent);
                        nav.setY(y[0]);

                    }
                }
            });
        }

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    public void initView(View view) {

        sv = (ScrollView) findViewById(R.id.sv);
        setting = (ImageView) findViewById(R.id.setting);
        info = (ImageView) findViewById(R.id.info);
        headphoto = (CircleImageView) findViewById(R.id.headphoto);
        introduction0 = (TextView) findViewById(R.id.introduction0);
        userName = (TextView) findViewById(R.id.userName);
        introduction = (TextView) findViewById(R.id.introduction);
        label = (TextView) findViewById(R.id.label);
        likesAndCollctions = (TextView) findViewById(R.id.likesAndCollctions);
        fans = (TextView) findViewById(R.id.fans);
        follows = (TextView) findViewById(R.id.follows);
        share = (ImageView) findViewById(R.id.share);
        userId = (TextView) findViewById(R.id.userId);
        nav = (TabLayout) findViewById(R.id.nav);
        vp = (ViewPager) findViewById(R.id.vp);
    }

    private View getview(String title) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.main_nav_top, null);
        TextView tv = view.findViewById(R.id.tv);
        tv.setText(title);
        return view;
    }

    private int getX() {
        int x = (int) (Math.random() * 200);
        return x;
    }

    private void getTrensItemData() {

        //动态
        List<RecContent> mTrensContentList = new ArrayList<>();
        mTrensContentList.add(new RecContent(1, R.drawable.yywtx8, "新人演唱", R.drawable.yyw8, "新人演唱海底,绝了", getX(), 666));
        mTrensContentList.add(new RecContent(1, R.drawable.yywtx9, "小可爱", R.drawable.yyw9, "可可爱爱的", getX(), 666));
        mTrensContentList.add(new RecContent(1, R.drawable.yywtx10, "绝对不是这个姿势", R.drawable.yyw10, "姿势帅才行", getX(), 666));
        mTrensContentList.add(new RecContent(1, R.drawable.yywtx11, "datang", R.drawable.yyw11, "不要以为我是猪", getX(), 666));
        mTrensContentList.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        mTrensContentList.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        mTrensContentList.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        mTrensContentList.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        mTrensContentList.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        mTrensContentList.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));

        //收藏
        List<RecContent> mTrensContentList2 = new ArrayList<>();
        mTrensContentList2.add(new RecContent(1, R.drawable.yywtx8, "新人演唱", R.drawable.yyw8, "新人演唱海底,绝了", getX(), 666));
        mTrensContentList2.add(new RecContent(1, R.drawable.yywtx9, "小可爱", R.drawable.yyw9, "可可爱爱的", getX(), 666));
        mTrensContentList2.add(new RecContent(1, R.drawable.yywtx10, "绝对不是这个姿势", R.drawable.yyw10, "姿势帅才行", getX(), 666));
        mTrensContentList2.add(new RecContent(1, R.drawable.yywtx11, "datang", R.drawable.yyw11, "不要以为我是猪", getX(), 666));
        mTrensContentList2.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        mTrensContentList2.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        mTrensContentList2.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));

        //赞过
        List<RecContent> mTrensContentList3 = new ArrayList<>();
        mTrensContentList3.add(new RecContent(1, R.drawable.yywtx8, "新人演唱", R.drawable.yyw8, "新人演唱海底,绝了", getX(), 666));
        mTrensContentList3.add(new RecContent(1, R.drawable.yywtx9, "小可爱", R.drawable.yyw9, "可可爱爱的", getX(), 666));
        mTrensContentList3.add(new RecContent(1, R.drawable.yywtx10, "绝对不是这个姿势", R.drawable.yyw10, "姿势帅才行", getX(), 666));
        mTrensContentList3.add(new RecContent(1, R.drawable.yywtx11, "datang", R.drawable.yyw11, "不要以为我是猪", getX(), 666));
        mTrensContentList3.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        mTrensContentList3.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        mTrensContentList3.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        lists.add(mTrensContentList);
        lists.add(mTrensContentList2);
        lists.add(mTrensContentList3);
    }
}
